#include "mem_test.h"
#include "2440lib.h"

unsigned char Membuf[4*1024];
unsigned int *pMemSpace = (unsigned int *)0x32000000;
unsigned int *pMemData = (unsigned int *)Membuf;
void MemWRC(void)//Memory Write Read And Compare
{
	unsigned int i,j=0;
	pMemSpace = (unsigned int *)0x32000000;
	do{	
		pMemData = (unsigned int *)Membuf;//reset the pointer
		//write MemData to MemSpace
		for (i = 0; i < 1024; i++)
		{
			*pMemSpace++ = *pMemData++;
		}
		pMemSpace -= 1024;//reset the pointer
		pMemData = (unsigned int *)Membuf;//reset the pointer
		//read Data from MemSpace and compare to Membuf which writed in
		for(i = 0; i < 1024; i++)
		{
			if(*pMemData++ != *pMemSpace++)
			{
				Uart_Printf("Memory Test error, error addr: 0x%x\n", pMemSpace);
				while(1);
			}
		}
		if((j++) % 256 == 0)
		{
			Uart_Printf("Test 1M OK!!! pMemSpace = %x\n", (unsigned int)pMemSpace);
		}
	}while((unsigned int)pMemSpace < 0x33ff0000);//don't flush stack space
}
void MemTest(void)
{
	unsigned int i;
	Uart_Printf("Memory Positive Testing...\n");
	//write aaaa5555aaaa5555... to Membuf
	for (i = 0; i < 1024; i++)
	{
		*pMemData++ = 0xaaaa5555;
	}
	MemWRC();
	Uart_Printf("Memory Negative Testing...\n");
	//write 5555aaaa5555aaaa... to Membuf
	for (i = 0; i < 1024; i++)
	{
		*pMemData++ = 0x5555aaaa;
	}
	MemWRC();

	Uart_Printf("Memory Test OK!!!\n");
}

